/*
翻转链表
连接：https://leetcode.cn/problems/reverse-linked-list/description/
题目：
简介：递归解题
*/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
       ListNode* tail =nullptr;
       ListNode* root=nullptr;
       dfs(head,tail,root);
       return root;
    }
    void dfs(ListNode*head,ListNode* tail,ListNode*&root)
    {
        if(head==nullptr)
        {
            root = tail;
            return;
        }
        dfs(head->next,head,root);
        head->next = tail;
    }
};